package com.xiaoma.tpo.web.activity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xiaoma.tpo.domain.Activity;
import com.xiaoma.tpo.domain.ActivityType;
import com.xiaoma.tpo.domain.UserKey;
import com.xiaoma.tpo.dto.ActivityDto;
import com.xiaoma.tpo.dto.ActivityVo;
import com.xiaoma.tpo.service.ActivityService;
import com.xiaoma.tpo.service.ActivityTypeService;
import com.xiaoma.tpo.service.UserKeyService;
import com.xiaoma.verbal.utils.DateUtil;

@Controller
@RequestMapping("activityManage")
public class ActivityController {
	
	@Autowired
	private ActivityService activityService;
	
	@Autowired
	private ActivityTypeService activityTypeService;
	
	@Autowired
	private UserKeyService userKeyService;
	
	/**
	 * 列表
	 * @param page
	 * @param rows
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/list")
	@ResponseBody
	public Map<String, Object> activityList(Integer page, Integer rows,
			HttpServletRequest request, ModelMap model) {
		Map<String, Object> result = new HashMap<String, Object>();
		
		if(page == null || page == 0) {
			page = 1;
			rows = 50;
		}
		List<ActivityDto> list = activityService.findAll(page, rows);
		result.put("rows", list);
		result.put("total", activityService.findAll().size());
		return result;
	}
	
	/**
	 * 新增或修改
	 * @param activityVo
	 * @return
	 */
	@RequestMapping(value = "/add")
	@ResponseBody
	public Map<String, Object> addActivity(ActivityVo activityVo) {
		
		Map<String, Object> result = new HashMap<String, Object>();
		Activity activity = new Activity();
		try {
			if(activityVo.getId() != null) {
				activity.setId(activityVo.getId());
			}
			activity.setBudget(activityVo.getBudget());
			activity.setStartDate(DateUtil.strToDate(activityVo.getStartDate(), "yyyy-MM-dd HH:mm"));
			activity.setEndDate(DateUtil.strToDate(activityVo.getEndDate(), "yyyy-MM-dd HH:mm"));
			activity.setActivityTypeId(activityVo.getActivityTypeId());
			activity.setTitle(activityVo.getTitle());
			activity.setIsDelete(0);
			activityService.save(activity);
			result.put("status", "SUCCESS");
		} catch (Exception e) {
			e.printStackTrace();
			result.put("status", "FAIL");
		}
		return result;
	}
	
	/**
	 * 新增或修改前页面跳转
	 * @param activityId
	 * @param result
	 * @return
	 */
	@RequestMapping(value = "/toAddActivity")
	public String toAddActivity(@RequestParam(value = "activityId", required = false) Integer activityId,
			Map<String, Object> result) {
		
		List<ActivityType> list = activityTypeService.findAll();
		Activity activity = new Activity();
		if(activityId != null) {
			activity = activityService.findById(activityId);
		}
		result.put("typeList", list);
		result.put("activity", activity);
		return "activity/activityAdd";
	}
	
	@ResponseBody
	@RequestMapping(value = "/exchangKey")
	public Map<String, Object> exchangKey(@RequestParam(value = "key") String key) {
		
		Map<String, Object> result = new HashMap<String, Object>();
		Integer money = 0;
		UserKey userKey = userKeyService.findByKey(key);
		if(userKey != null) {
			if(userKey.getStatus() == 0) {
				money =  10;
				result.put("status", 1);
				result.put("message", money);
			}else {
				result.put("status", 0);
				result.put("message", "该兑换码已领取");
			}
		}else {
			result.put("status", 0);
			result.put("message", "该兑换码不存在");
		}
		return result;
	}
	
	@ResponseBody
	@RequestMapping("/removeActivity")
	public Map<String, Object> removeActivity(@RequestParam(value = "id") Integer id) {
		Map<String, Object> result = new HashMap<String, Object>();
		Activity activity = activityService.findById(id);
		activity.setIsDelete(1);
		activityService.save(activity);
		return result;
	}
	
}
